﻿using UnityEngine;
using System.Collections;

public class ModelVisible : MonoBehaviour {
	static public ModelVisible Get(GameObject model){
		ModelVisible mv = null;
		Renderer r = model.GetComponentInChildren<Renderer> ();
		if (r != null) {
			mv = r.GetComponent<ModelVisible> ();
			if (mv == null)
				mv = r.gameObject.AddComponent<ModelVisible> ();
		}
		return mv;
	}
	static public void Despawn(GameObject model){
		ModelVisible mv = model.GetComponentInChildren<ModelVisible> ();
		if (mv != null) {
			GameObject.Destroy (mv);
		}
	}

	public VoidDelegate _onBecameVisible;
	public VoidDelegate _onBecameInvisible;
	public void Register(VoidDelegate Visible,VoidDelegate Invisible){
		_onBecameVisible = Visible;
		_onBecameInvisible = Invisible;
	}
	void OnBecameInvisible () {
		if (_onBecameInvisible != null)
			_onBecameInvisible ();
		enabled = false;
	}
	void OnBecameVisible(){
		if (_onBecameVisible != null)
			_onBecameVisible ();
		enabled = true;
	}

}
